﻿/*
    Autor : Roger Medeiros
    Data  : 01/02/2012
    Site  : www.interacaosistemas.com.br
*/


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using i9PgAdmin.Core;
using System.Threading;

namespace i9PgAdmin
{
    public partial class FrontEnd : Form
    {
        private bool Busy = false;
        private bool Abriu = false;

        public FrontEnd()
        {
            InitializeComponent();
        }

        private void TextoStatus(string Texto)
        {
            this.lblStatus.Text = string.IsNullOrEmpty(Texto) ? "Pronto!" : Texto;
        }

        private void DumpDatabase()
        {
            if (this.Busy)
                return;
            this.Busy = true;
            this.TextoStatus("Criando Dump...");
            Application.DoEvents();            
            var dump = new DumpExec();

            this.Hide();

            if (dump.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
            {
                this.Busy = false;
                this.TextoStatus("");
                this.Show();
                return;
            }
            this.Show();
            this.Update();
            var backup = new Dump();
            backup.conexao.Carrega();
            //backup.conexao.PegaCaminhoBin();
            backup.IniciaDump();
            backup.conexao.Salva();
            Application.DoEvents();
            this.Busy = false;
            this.TextoStatus("");
        }

        private void Agendamento()
        {
            var form = new FrmAgenda();
            this.Hide();
            form.ShowDialog(this);
            this.Show();
        }

        private void imgDump_Click(object sender, EventArgs e)
        {
            this.DumpDatabase();
        }

        private void FrontEnd_Load(object sender, EventArgs e)
        {
            this.TextoStatus("");
            this.timer.Enabled = true;
        }

        private void imgSair_Click(object sender, EventArgs e)
        {
            if (this.Busy)
                return;
            this.Hide();
        }

        private void imgSobre_Click(object sender, EventArgs e)
        {
            this.Hide();
            var sobre = new FrmSobre();
            sobre.ShowDialog();
            this.Show();
        }

        private void imgVacuum_Click(object sender, EventArgs e)
        {
            if (this.Busy)
                return;

            if (MessageBox.Show("Confirma a execução da Manutenção?", "i9PgAdmin", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
                return;

            this.Busy = true;
            this.TextoStatus("Efetuando Manutenção no Database...");
            Application.DoEvents();
            var analyze = new VacuumAnalyze();
            analyze.conexao.Carrega();
            analyze.conexao.PegaCaminhoBin();
            analyze.IniciaProcesso();
            Application.DoEvents();
            this.Busy = false;
            this.TextoStatus("");
        }

        private void btAgenda_Click(object sender, EventArgs e)
        {
            this.Agendamento();
        }

        private void VerificaAgendamentos()
        {
            var agenda = new Agenda();
            agenda.Carrega();
            var HoraAtual = DateTime.Now.Hour.ToString().PadLeft(2, '0') + DateTime.Now.Minute.ToString().PadLeft(2, '0');

            if (HoraAtual != (agenda.Horas + agenda.Minutos))
                return;

            var diaSemana = DateTime.Now.DayOfWeek;

            var Rodar = ((diaSemana == DayOfWeek.Monday && agenda.Segunda) ||
                            (diaSemana == DayOfWeek.Tuesday && agenda.Terca) ||
                            (diaSemana == DayOfWeek.Wednesday && agenda.Quarta) ||
                            (diaSemana == DayOfWeek.Thursday && agenda.Quinta) ||
                            (diaSemana == DayOfWeek.Friday && agenda.Sexta) ||
                            (diaSemana == DayOfWeek.Saturday && agenda.Sabado) ||
                            (diaSemana == DayOfWeek.Sunday && agenda.Domingo));

            if (!Rodar)
                return;

            var objThread = new Thread(new ThreadStart(BackupAgendado));
            objThread.IsBackground = true;
            objThread.Priority = ThreadPriority.Normal;
            objThread.Start();
        }

        private void BackupAgendado()
        {
            if (this.Busy)
                return;
            this.Busy = true;
            this.TextoStatus("Criando Dump via Agendamento...");
            var dump = new DumpExec();
            var backup = new Dump();
            var agenda = new Agenda();
            agenda.Carrega();
            backup.conexao.Carrega();
            if (!string.IsNullOrEmpty(agenda.Caminho))
                backup.conexao.CaminhoDump = agenda.Caminho;
            //backup.conexao.PegaCaminhoBin();
            backup.Comprimir = agenda.Zipar;
            backup.IniciaDump();

            this.Busy = false;
            this.TextoStatus("");
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.VerificaAgendamentos();
        }

        private void FrontEnd_Activated(object sender, EventArgs e)
        {
            if (!this.Abriu)
            {
                this.tray.Visible = true;
                this.tray.ShowBalloonTip(1000);
                this.Hide();
                this.Abriu = true;
            }
        }

        private void sairToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!this.Busy)
                Application.Exit();
        }

        private void restaurarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Show();
        }
    }
}
